package com.hyl.authdemo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hyl.authdemo.entity.UserTokenVo;
import com.hyl.authdemo.util.CommenCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/7/8 10:44
 **/
@Service
public class TokenServiceImpl implements TokenService {

	@Resource
	RedisService redisService;

	@Override
	public UserTokenVo saveToken(String username) {
		String key = CommenCode.USER_LOGIN_token +username;
		UserTokenVo vo = new UserTokenVo();
		vo.setToken(key);
		vo.setUsername(username);
		redisService.set(key, JSON.toJSONString(vo));
		return vo;
	}

	@Override
	public boolean isExist(String token) {
		String loginStringJson = redisService.get(token);
		if(loginStringJson!=null) {
			return true;
		}
		return false;
	}

	@Override
	public UserTokenVo findByToken(String token) {
		String loginStringJson = redisService.get(token);
		if(loginStringJson!=null) {
			UserTokenVo vo = (UserTokenVo) JSONObject.parse(loginStringJson);
			return vo;
		}
		return null;
	}
}
